/*
  Copyright � 2009 Smart Enterprise Solution Corp.
  Email: contact@ses.vn - Website: http://www.ses.vn
  ThanhNam ERP Project.
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
using SES.CMS.DO;
using System.Collections.Generic;
/// <summary>
/// Summary description for CounterDAL
/// </summary>
namespace SES.CMS.DAL
{

    public class CounterDAL : BaseDAL
    {
        #region Private Variables

        #endregion

        #region Public Constructors
        public CounterDAL()
        {
            //
            // TODO: Add constructor logic here
            //

        }
        #endregion



        #region Public Methods
        public int Insert(CounterDO objCounterDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spCounter_Insert";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@IP", SqlDbType.NVarChar);
            Sqlparam.Value = objCounterDO.IP;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Date", SqlDbType.DateTime);
            Sqlparam.Value = objCounterDO.Date;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ID", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);


            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ID"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ID"].Value);

            return result;
        }

        public int Update(CounterDO objCounterDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spCounter_UpdateByPK";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@CounterID", SqlDbType.Int);
            Sqlparam.Value = objCounterDO.CounterID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@IP", SqlDbType.NVarChar);
            Sqlparam.Value = objCounterDO.IP;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@Date", SqlDbType.DateTime);
            Sqlparam.Value = objCounterDO.Date;
            Sqlcomm.Parameters.Add(Sqlparam);



            Sqlparam = new SqlParameter("@ErrorCode", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);

            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ErrorCode"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ErrorCode"].Value);

            return result;


        }

        public int Delete(CounterDO objCounterDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spCounter_DeleteByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@CounterID", SqlDbType.Int);
            Sqlparam.Value = objCounterDO.CounterID;
            Sqlcomm.Parameters.Add(Sqlparam);



            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public int DeleteAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spCounter_DeleteAll";

            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public CounterDO Select(CounterDO objCounterDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spCounter_GetByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@CounterID", SqlDbType.Int);
            Sqlparam.Value = objCounterDO.CounterID;
            Sqlcomm.Parameters.Add(Sqlparam);



            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["CounterID"]))
                    objCounterDO.CounterID = Convert.ToInt32(dr["CounterID"]);
                if (!Convert.IsDBNull(dr["IP"]))
                    objCounterDO.IP = Convert.ToString(dr["IP"]);
                if (!Convert.IsDBNull(dr["Date"]))
                    objCounterDO.Date = Convert.ToDateTime(dr["Date"]);

            }
            return objCounterDO;
        }

        public ArrayList SelectAll1()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spCounter_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;
            ArrayList arrCounterDO = new ArrayList();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    CounterDO objCounterDO = new CounterDO();
                    if (!Convert.IsDBNull(dr["CounterID"]))
                        objCounterDO.CounterID = Convert.ToInt32(dr["CounterID"]);
                    if (!Convert.IsDBNull(dr["IP"]))
                        objCounterDO.IP = Convert.ToString(dr["IP"]);
                    if (!Convert.IsDBNull(dr["Date"]))
                        objCounterDO.Date = Convert.ToDateTime(dr["Date"]);
                    arrCounterDO.Add(objCounterDO);
                }
            }
            return arrCounterDO;
        }
        public List<CounterDO> GetAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spCounter_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;
            List<CounterDO> lstCounter = new List<CounterDO>();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    CounterDO objCounterDO = new CounterDO();
                    if (!Convert.IsDBNull(dr["CounterID"]))
                        objCounterDO.CounterID = Convert.ToInt32(dr["CounterID"]);
                    if (!Convert.IsDBNull(dr["IP"]))
                        objCounterDO.IP = Convert.ToString(dr["IP"]);
                    if (!Convert.IsDBNull(dr["Date"]))
                        objCounterDO.Date = Convert.ToDateTime(dr["Date"]);
                    lstCounter.Add(objCounterDO);
                }
            }
            return lstCounter;
        }

        public DataTable SelectAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spCounter_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }


        #endregion

    }

}
